package com.edu.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.edu.dao.UserDao;
import com.edu.dao.Impl.UserDaoImpl;
import com.edu.entity.User;
import com.edu.service.UserService;
import com.edu.service.Impl.UserServiceImpl;

/**
 * Servlet implementation class UserServletAll
 */
@WebServlet("/UserServletAll")
public class UserServletAll extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserServletAll() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("1111111111");
		request.setCharacterEncoding("UTF-8");
		String menu = request.getParameter("menu");
		System.out.println(menu);
		//查询
		if ("1".equals(menu)) {
			queryByUser(request,response);
		//修改
		}else if("2".equals(menu)) {
			userUpdate(request, response);
		//添加
		}else if("3".equals(menu)) {
			userAdd(request, response);
		//删除
		}else if("4".equals(menu)) {
			userDelete(request, response);
		}else {
			System.out.println("sorry!");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	//查询
	protected void queryByUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		int id = -1;
		if(!("".equals(request.getParameter("id")))){
			id = Integer.parseInt(request.getParameter("id"));
		}
		String username = request.getParameter("username");
		String userpass = request.getParameter("userpass");
		String sex = request.getParameter("sex");
		String phone = request.getParameter("phone");
		User user = new User(id,username,userpass,sex,phone);
		UserService userService = new UserServiceImpl();
		List<User> list = userService.selectByUser(user);
		request.setAttribute("list",list);
		//response.sendRedirect("index.jsp");
		request.getRequestDispatcher("individual.jsp").forward(request, response);
	}
	
	//修改
	protected void userUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		int id = Integer.parseInt(request.getParameter("id"));
		String username = request.getParameter("username");
		String userpass = request.getParameter("userpass");
		String sex = request.getParameter("sex");
		String phone = request.getParameter("phone");
		User user = new User(id,username,userpass,sex,phone);
		UserService userService = new UserServiceImpl();
		userService.updateUser(user);
		response.sendRedirect("individual.jsp");
	}
	
	//添加
	protected void userAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		String username = request.getParameter("username");
		String userpass = request.getParameter("userpass");
		String sex = request.getParameter("sex");
		String phone = request.getParameter("phone");
		User user = new User(username,userpass,sex,phone);
		UserService userService = new UserServiceImpl();
		userService.addUser(user);
		response.sendRedirect("individual.jsp");
		
//		request.setCharacterEncoding("UTF-8");
//		String username = request.getParameter("username");//获取用户名
//		String userpass = request.getParameter("userpass");//获取密码
//		String sex = request.getParameter("sex");//获取性别
//		String phone = request.getParameter("phone");//获取电话号码
//		User user = new User(username,userpass,sex,phone);//创建User对象
//		UserDao userDao = new UserDaoImpl();//创建UserDaoImpl对象
//		
//		
//		int rowNum = userDao.add(user);//调用UserDaoImpl中的register（）方法进行注册，返回受影响行数
//		System.out.println(rowNum);
//		
//		if (rowNum > 0) {//如果注册成功，则重定向到登录页面
//			request.getRequestDispatcher("login.jsp").forward(request, response);
//		}else {//否则重定向到注册页面
//			System.out.println("错误");
//			response.sendRedirect("register.jsp");
//		}
	}
	
	//删除
	protected void userDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		String[] ids = request.getParameterValues("selectUid");

		UserService userService = new UserServiceImpl();
		userService.deleteUserAll(ids);
		response.sendRedirect("individual.jsp");
	}

}
